package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.service.IAlbumService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/album")
@Api(tags = "04. 相册管理模块")
@Slf4j
@Validated
public class AlbumController {
    @Autowired
    private IAlbumService albumService;

    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加相册】的请求，参数：{}", albumAddNewParam);
        albumService.addNew(albumAddNewParam);
        log.debug("处理【添加相册】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除数据")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "相册ID",required = true,dataType = "long"),

    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除相册失败，请提交合法的ID值！")
                         @RequestParam Long id) {
        log.debug("开始处理【根据id删除相册】的请求，参数：{}", id);
        albumService.delete(id);
        log.debug("处理【根据id删除相册】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@Range(min = 1,message = "根据ID更新相册失败，请提交合法的ID值！")
                                     @RequestParam Long id,
                                 @Valid AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册】的请求，参数：{}", id, albumUpdateInfoParam);
        albumService.updateInfoById(id, albumUpdateInfoParam);
        log.debug("处理【修改相册】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据id查询相册")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "相册id",required = true,dataType = "Long",paramType = "query")
    })
    public JsonResult getStandardById(Long id) {
        log.debug("开始处理【根据id查询相册】的请求，id：{}",id);
        AlbumStandardVO standardById = albumService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list")
    @ApiOperation("查询相册")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false,dataType = "Integer",paramType = "query")
    })
    public JsonResult selectListItem(@Range(min = 1,message = "查询相册失败,请提供正确的页码值！") Integer pageNum,String queryType) {
        log.debug("开始处理【查询相册列表】的请求，页码：{}",pageNum);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<AlbumListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData =  albumService.selectListItem(pageNum,Integer.MAX_VALUE);
        } else {
            pageData =  albumService.selectListItem(pageNum);
        }

        return JsonResult.ok(pageData);
    }

}
